#	IMX6 L4.1.15 BSP build tools
#
#

ROOT_OBJ	:= $(shell pwd)
ROOT_DEV	:= $(ROOT_OBJ)/..

SRC_KERNEL	:= $(ROOT_DEV)/zy_kernel
SRC_UBOOT	:= $(ROOT_DEV)/zy_u-boot

OBJ_KERNEL	:= $(ROOT_OBJ)/obj_kernel
OBJ_UBOOT	:= $(ROOT_OBJ)/obj_u-boot

CFG_KERNEL	:= $(ROOT_OBJ)/cfg_kernel
CFG_UBOOT	:= $(ROOT_OBJ)/cfg_u-boot

INSTALL_MOD	:= $(ROOT_DEV)/modules
INSTALL_HDR	:= $(ROOT_DEV)/headers
INSTALL_OUT	:= $(ROOT_DEV)/images

INSTALL = /usr/bin/install -m 0664
RM		= rm -rf
CP		= cp -rfv
TAR		= fakeroot tar
MKDIR	= mkdir -p

TARGETS	:= \
	kernel \
	headers \
	uboot

DTBS	:= \
	imx6q-d7.dtb \
	imx6q-d7a.dtb \
	imx6q-d20.dtb \
	imx6q-d10.dtb \
	imx6q-d4.dtb

ifeq ($(strip $(SDKTARGETSYSROOT)),)
$(error You should initial sdk toolchain environment first)
endif

all:$(TARGETS)

kernel:zImage dtbs modules

imx_d7_defconfig:
imx_d7_mfg_defconfig:
%_defconfig:$(SRC_KERNEL)/arch/$(ARCH)/configs/%_defconfig
	@echo $(@:imx_%_defconfig=%) > $(CFG_KERNEL)
	@$(MKDIR) $(OBJ_KERNEL)
	@$(CP) $< $(OBJ_KERNEL)/.config
	$(MAKE) -C $(SRC_KERNEL) O=$(OBJ_KERNEL) oldnoconfig

zImage_mfgtool:
zImage:$(OBJ_KERNEL)/.config
	$(MAKE) -C $(OBJ_KERNEL) -j4 zImage CC=arm-poky-linux-gnueabi-gcc LD=arm-poky-linux-gnueabi-ld.bfd LOADADDR=0x10008000
	@$(INSTALL) $(OBJ_KERNEL)/arch/$(ARCH)/boot/zImage $(INSTALL_OUT)/$@


dtbs:$(DTBS)


imx6q-d7.dtb:
imx6q-d7a.dtb:
imx6q-d20.dtb:
imx6q-d10.dtb:
imx6q-d4.dtb:
%.dtb:
	$(MAKE) -C $(OBJ_KERNEL) $@
	@$(INSTALL) $(OBJ_KERNEL)/arch/$(ARCH)/boot/dts/$@ $(INSTALL_OUT)/$@

modules:$(OBJ_KERNEL)/arch/$(ARCH)/boot/zImage
	@$(RM) $(INSTALL_MOD)/* -rf
	$(MAKE) -C $(OBJ_KERNEL) -j4 modules LD=arm-poky-linux-gnueabi-ld.bfd
	$(MAKE) -C $(OBJ_KERNEL) modules_install INSTALL_MOD_PATH=$(INSTALL_MOD)
	@$(TAR) jcvf $(INSTALL_OUT)/$@.tar.bz2 -C $(INSTALL_MOD)/ .

headers:$(OBJ_KERNEL)/.config
	$(RM) $(INSTALL_HDR)/* -rf
	$(MAKE) -C $(OBJ_KERNEL) headers_install INSTALL_HDR_PATH=$(INSTALL_HDR)

kclean:
	@$(RM) $(INSTALL_OUT)/zImage
	@$(RM) $(INSTALL_OUT)/*.dtb
	@$(RM) $(OBJ_KERNEL)

uboot:u-boot

mx6q_d7_config:
mx6q_d7a_config:
mx6q_d20_config:
mx6q_d10_config:
mx6q_d4_config:
mx6q_d7_mfg_config:
%_config:
	@echo $(@:mx6q_%_config=%) > $(CFG_UBOOT)
	@$(MKDIR) $(OBJ_UBOOT)
	$(MAKE) -C $(SRC_UBOOT) O=$(OBJ_UBOOT) $@

u-boot:$(OBJ_UBOOT)/.config $(CFG_UBOOT)
	$(MAKE) -C $(OBJ_UBOOT) -j4 u-boot.imx
	@$(CP) -v $(OBJ_UBOOT)/u-boot.imx $(INSTALL_OUT)/u-boot.imx
	@$(CP) -v $(OBJ_UBOOT)/u-boot.imx $(INSTALL_OUT)/u-boot-`cat $(CFG_UBOOT)`.imx

uclean:
	@$(RM) $(INSTALL_OUT)/u-boot.imx
	@$(RM) $(OBJ_UBOOT)


